// pages/view/drug/drugConsumption/drugConsumption.js
// pages/view/drug/treasuryDrugs/treasuryDrugs.js
var wxCharts = require('../../../../utils/wxcharts.js');
const network = require('../../../../utils/network.js')
var app = getApp();
var columnChart = null;
var pageSize = 0;
var startTime_n;
var endTime_n;
var deptId_n;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headers: [{ text: 'rowCount', display: '排名' }, { text: 'itemCode', display: '代码' }, { text: 'itemName', display: '名称' }, { text: 'standard', display: '规格' }, { text: 'unitName', display: '单位' }, { text: 'quantity', display: '数量' }, { text: 'amount', display: '金额' }, { text: 'proportion', display: '比例' }],
    row: [],
    current: 1,
    totalPage: 1,
    amount:-2,

    //药品消耗最大前10位-柱形图
    consumption_categories: [],
    consumption_data: [],

  },
  onChange(e) {
    console.log('onChange', e)
    this.setData({
      current: e.detail.current,
    })

    this.myRequest(e)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    console.log('options:', options)
    var windowheight = wx.getSystemInfoSync().windowHeight;
    var tdHeight = [18.4, 21.6, 39]
    if (windowheight < 550) {
      pageSize = Math.round(windowheight * 0.8 / tdHeight[0]);
    } else if (windowheight < 850) {
      pageSize = Math.round(windowheight * 0.8 / tdHeight[1]);
    } else {
      pageSize = Math.round(windowheight * 0.8 / tdHeight[2]);
    }
    console.log("每页个数", pageSize);
    //调用封装好的网络请求，获取数据
    network.get('/drug/consumption?startTime=' + options.startTime + '&endTime=' + options.endTime + '&deptId=' + options.deptId + '&pageCount=' + that.data.current  + '&pageSize=' + pageSize, {})
    .then(function (res) {
      console.log('res:', res)
      var obj1 = res.data.data
      var obj2 = res.data.data_2.list1
      var obj3 = res.data.data_2.list2
      that.setData({
        startTime: startTime_n,
        endTime: endTime_n,
        msg: res.data.errMsg,
        row: res.data.data,
        totalPage: res.data.totalPage,
        amount: res.data.data_3.amount,
        //入库金额最大前10位-柱形图
        consumption_categories: obj2,
        consumption_data: obj3,
      })
    }).then(function (res) {
      wx.hideLoading()

      if (Object.keys(that.data.consumption_data).length === 0) {
        wx.showModal({

          title: '提示',

          content: '请求的数据为空，请重新选择时间',

        })
      }else{
        that.createColumnChart();
      }
    
     

    }).catch(function (res) {
      console.log(res)
    })

    startTime_n = options.startTime
    endTime_n = options.endTime
    deptId_n = options.deptId
    //this.myRequest(options);
  },
  myRequest: function (options) {
    var that = this;
    //console.log('options:', options)
    //获得表格高度
    var windowheight = wx.getSystemInfoSync().windowHeight;
    var tdHeight = [18.4, 21.6, 39]
    if (windowheight < 550) {
      pageSize = Math.round(windowheight * 0.8 / tdHeight[0]);
    } else if (windowheight < 850) {
      pageSize = Math.round(windowheight * 0.8 / tdHeight[1]);
    } else {
      pageSize = Math.round(windowheight * 0.8 / tdHeight[2]);
    }
    console.log("每页个数", pageSize);
    //调用封装好的网络请求，获取数据
    network.get('/drug/consumption?startTime=' + startTime_n + '&endTime=' + endTime_n + '&deptId='+ deptId_n+ '&pageCount=' + that.data.current + '&pageSize=' + pageSize, {}).then(function (res) {
      console.log('res:', res)

      that.setData({
        startTime: startTime_n,
        endTime: endTime_n,
        msg: res.data.errMsg,
        row: res.data.data,
        amount: res.data.data_3.amount,
        totalPage: res.data.totalPage
        
      })
      console.log('row:', that.data.row)

    }).then(function (res) {

      //that.createColumnChart();

    }).catch(function (res) {
      console.log(res)
    })
  },
   /**
   * 创建柱形图
   */
  createColumnChart: function (e) {
    var that = this;
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    columnChart = new wxCharts({
      canvasId: 'columnCanvas',
      type: 'column',
      animation: true,
      categories: that.data.consumption_categories,

      series: [{
        name: '药品入库金额',
        data: that.data.consumption_data,
        format: function (val, name) {

          return val.toFixed(2);
        }
      }],
      yAxis: {
        format: function (val) {
          return val ;
        },
        title: '金额（元）',
        min: 0
      },
      xAxis: {
        disableGrid: false,
        type: 'calibration'
      },
      extra: {
        column: {
          width: 15
        }
      },
      width: 650,
      height: 550,
    });
  },
})